package fulbiNet.dao;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.sql.Connection;
import fulbiNet.execpcion.DAOExcepcion;
import fulbiNet.modelo.Alquiler;
import fulbiNet.modelo.Servicio;
import fulbiNet.util.ConexionBD;

	public class AlquilerDAO extends BaseDAO {

		public void insertar(Alquiler vo) throws DAOExcepcion {
			System.out.println("AlquilerDAO: insertar(Alquiler vo)");
			String query = "INSERT INTO Alquiler(fechahora,horas,idCliente,IdCancha) VALUES (?,?,?,?)";
			Connection con = null;
			PreparedStatement stmt = null;
			try {
				con = ConexionBD.obtenerConexion();
				stmt = con.prepareStatement(query);
				
				stmt.setDate(1, vo.getFechahora());
				stmt.setInt(2, vo.getHoras());
				stmt.setInt(3, vo.getoCliente().getCodigo());
				stmt.setInt(4, vo.getoCancha().getCodigo());

				int i = stmt.executeUpdate();
				if (i != 1) {
					throw new SQLException("No se pudo insertar");
				}
				
				query = "select MAX(idAlquiler) from Alquiler";
				stmt = con.prepareStatement(query);
				ResultSet rs = stmt.executeQuery();
				if (rs.next())
				{
					vo.setNumero(rs.getInt(1));
				}
				
			} catch (SQLException e) {
				System.err.println(e.getMessage());
				throw new DAOExcepcion(e.getMessage());
			} finally {
				this.cerrarStatement(stmt);
				this.cerrarConexion(con);
			}
		}
		
		public void insertarDetalle(Alquiler vo, Servicio so) throws DAOExcepcion {
			System.out.println("AlquilerDAO: insertar(Alquiler vo)");
			String query = "INSERT INTO DetalleAlquiler(idAlquiler,idServicio) VALUES (?,?)";
			Connection con = null;
			PreparedStatement stmt = null;
			try {
				con = ConexionBD.obtenerConexion();
				stmt = con.prepareStatement(query);
				
				stmt.setInt(1, vo.getNumero());
				stmt.setInt(2, so.getCodigo());

				int i = stmt.executeUpdate();
				if (i != 1) {
					throw new SQLException("No se pudo insertar");
				}
			} catch (SQLException e) {
				System.err.println(e.getMessage());
				throw new DAOExcepcion(e.getMessage());
			} finally {
				this.cerrarStatement(stmt);
				this.cerrarConexion(con);
			}
		}
		
	public Collection<Alquiler> buscar(Timestamp dfechahora, Timestamp hfechahora, int horas, int oCliente, int oCancha) throws DAOExcepcion {
		
		System.out.println("CanchaDAO: buscar()");
		Collection<Alquiler> c = new ArrayList<Alquiler>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "SELECT Alquiler.idAlquiler,Alquiler.fechahora, Alquiler.horas, Alquiler.idCliente, Alquiler.idCancha from Alquiler WHERE Alquiler.idCliente = ? and Alquiler.idCancha = ? and Alquiler.fechahora between ? and ? and horas = ?";
			stmt = con.prepareStatement(query);
		
			stmt.setInt(1, oCliente);
			stmt.setInt(2, oCancha);
			stmt.setTimestamp(3, dfechahora);
			stmt.setTimestamp(4, hfechahora);
			
			
			rs = stmt.executeQuery();
			while (rs.next()) {
				Alquiler vo = new Alquiler();
				
				vo.setNumero(rs.getInt(1));
				vo.setFechahora(rs.getDate(2));
				vo.setHoras(rs.getInt(3));

				c.add(vo);
			}
			
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}
	
	public Collection<Alquiler> buscarReservada(Alquiler al) throws DAOExcepcion {
		System.out.println("CanchaDAO: buscarReservada()");
		Collection<Alquiler> c = new ArrayList<Alquiler>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "SELECT idAlquiler, fechahora, horas, idCliente, idCancha from Alquiler WHERE idCancha = ? and fechahora = ? and horas = ?";
			stmt = con.prepareStatement(query);
		
			stmt.setInt(1, al.getoCancha().getCodigo());
			stmt.setDate(2, al.getFechahora());
			stmt.setInt(3, al.getHoras());
			
			rs = stmt.executeQuery();
			while (rs.next()) {
				Alquiler vo = new Alquiler();
				
				vo.setNumero(rs.getInt(1));
				vo.setFechahora(rs.getDate(2));
				vo.setHoras(rs.getInt(3));

				c.add(vo);
			}
			
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}

	public void eliminar(int idAlquiler) throws DAOExcepcion {
		System.out.println("AlquilerDAO: eliminar(int idAlquiler)");
		String query = "DELETE FROM Alquiler WHERE idAlquiler=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1,idAlquiler);
			int i = stmt.executeUpdate();
			System.out.print("aqui:"+i);
			if (i != 1) {
				
				//throw new SQLException("No se pudo eliminar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			//throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}
	}
